/**
* Html.js
*  
* @author Demis Bellot
* @version 1.0
* @requires Class.js
*
* Copyright 2009, Demis Bellot
* http://code.google.com/p/ajaxstack/
*/

function Html() { }
var H = Html;

H.tag = function(tagName, attrs, innerHtml)
{
	attrs = attrs || {};

	//If the only arg is a string, its actually the content
	if (is.ValueType(attrs) && !innerHtml)
	{
		innerHtml = attrs;
		attrs = null;
	}
	else if (attrs['dataSource'])
	{
		//every tag also supports a dataSource/onItemDataBound templating if supplied
		innerHtml = innerHtml || "";
		var dataSource = attrs['dataSource'];
		for (var i in dataSource)
		{
			innerHtml += attrs['onItemDataBound']
				? attrs['onItemDataBound'](dataSource[i])
				: dataSource[i].toString();
		}
		delete attrs['dataSource'];
		if (attrs['onItemDataBound']) delete attrs['onItemDataBound'];
	}

	var html = "<" + tagName;
	var attrStr = S.createAttr(attrs);
	if (attrStr) html += " " + attrStr;

	html += !innerHtml
		? "/>"
		: ">" + innerHtml + "</" + tagName + ">";

	return html;
};

H.tagNames = "span,div,p,label,a,b,i,em,code,pre,ul,ol,li,dl,dt,dd,h1,h2,h3,h4,h5,h6,br,img,input,button,form,fieldset,iframe,abbr,acronym,defn".split(',');
for (var i in H.tagNames)
{
	(function(tagName)
	{
		Html[tagName] = function(attrs, innerHtml)
		{
			return H.tag(tagName, attrs, innerHtml);
		};
	})(H.tagNames[i]);
}

H.inputText = function(attrs)
{
	attrs['type'] = "text";
	return H.tag("input", attrs);
};
H.inputCheckbox = function(attrs)
{
	attrs['type'] = "checkbox";
	return H.tag("input", attrs);
};
H.textArea = function(attrs)
{
	var value = attrs['value'];
	delete attrs['value'];
	return H.tag("textarea", attrs, value);
};
